package com.ehotting.edsta.circuitDesign.gen.circuit;

import com.ehotting.edsta.circuitDesign.enums.SceneTypeEnum;
import com.ehotting.edsta.circuitDesign.enums.StateTypeEnum;
import com.ehotting.edsta.circuitDesign.domain.EsCircuit;
import com.ehotting.edsta.circuitDesign.domain.EsRuleScript;
import com.ehotting.edsta.circuitDesign.gen.circuit.helper.CircuitScriptHelper;
import com.ehotting.edsta.circuitDesign.gen.circuit.script.AssignmentCircuitScript;
import com.ehotting.edsta.circuitDesign.gen.circuit.script.CircuitScriptAble;
import com.ehotting.edsta.circuitDesign.gen.circuit.script.ConditionCircuitScript;
import com.ehotting.edsta.circuitDesign.gen.circuit.script.CustomCircuitScript;
import com.ehotting.edsta.circuitDesign.gen.ScriptConstants;
import com.ehotting.edsta.circuitDesign.service.IEsRuleScriptService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

@Service
public class CircuitScriptFactory {

    private Logger logger = LoggerFactory.getLogger(CircuitScriptFactory.class);

    @Autowired
    private IEsRuleScriptService esRuleScriptService;
    @Autowired
    private CircuitScriptHelper circuitScriptHelper;
    @Autowired
    private AssignmentCircuitScript assignmentCircuitScript;
    @Autowired
    private ConditionCircuitScript conditionCircuitScript;
    @Autowired
    private CustomCircuitScript customCircuitScript;

    private List<CircuitScriptAble> codeList = new ArrayList();

    @PostConstruct
    public void init(){
        codeList.add(assignmentCircuitScript);
        codeList.add(conditionCircuitScript);
        codeList.add(customCircuitScript);
    }


    public String genMethod(EsCircuit circuit,  List<String>  argumentsList){
        StringBuffer buf = new StringBuffer();
        List<EsRuleScript> esRuleScriptList = esRuleScriptService.findBySceneIdAndTypeAndState(
                circuit.getCircuitType().getId(),
                SceneTypeEnum.CIRCUIT_SELECT,
                StateTypeEnum.YES);

        for(EsRuleScript script : esRuleScriptList){
            genScript( script,  buf,  argumentsList);
        }
        return buf.toString();
    }

    private void genScript(EsRuleScript script, StringBuffer buf, List<String> argumentsList){
        for (CircuitScriptAble code : codeList) {
            boolean isTrue = code.isTrue(script.getType());
            if (isTrue) {
                buf.append(code.invoke(script,argumentsList, circuitScriptHelper.genMethodName(script)));
                buf.append(ScriptConstants.LINE);
            }
        }
    }


    public String genCall(EsCircuit circuit, List<String>  inputParamList) {
        StringBuffer buf = new StringBuffer();
        List<EsRuleScript> scriptList = esRuleScriptService.findBySceneIdAndTypeAndState(
                circuit.getCircuitType().getId(),
                //circuit.getId(),
                //circuit.getCircuitTypeId(),
                SceneTypeEnum.CIRCUIT_SELECT,
                StateTypeEnum.YES);
        for(EsRuleScript esRule: scriptList){
            genCall( buf,esRule,   inputParamList);
        }
        return buf.toString();
    }

    private void genCall(StringBuffer buf, EsRuleScript esRuleScript, List<String> inputParamList){
        String var = circuitScriptHelper.genVar( esRuleScript);
        buf.append(ScriptConstants.TAB).append("def ").append(var).append(" = ");
        buf.append(circuitScriptHelper.genMethodNameAndArgument(circuitScriptHelper.genMethodName(esRuleScript),inputParamList)).append( ";").append(ScriptConstants.LINE);
        buf.append(ScriptConstants.TAB).append( "if("+var+" == false){").append(ScriptConstants.LINE);
        buf.append(ScriptConstants.TAB(2)).append( "return false;").append(ScriptConstants.LINE);
        buf.append(ScriptConstants.TAB).append( "}").append(ScriptConstants.LINE);
    }



}